<template>
  <div class="wechat-error">
    <div class="error-container">
      <div class="error-card">
        <div class="error-icon">
          <svg width="80" height="80" viewBox="0 0 24 24" fill="#FF4757">
            <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/>
          </svg>
        </div>
        
        <h1 class="error-title">登录失败</h1>
        <p class="error-message">{{ errorMessage }}</p>
        
        <div v-if="errorCode" class="error-details">
          <p><strong>错误代码：</strong>{{ errorCode }}</p>
        </div>
        
        <div class="action-buttons">
          <button @click="retryLogin" class="retry-btn">
            重新登录
          </button>
          <button @click="goToNormalLogin" class="normal-login-btn">
            普通登录
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { initVConsole } from '@/utils/vconsole'

const router = useRouter()
const route = useRoute()

const errorMessage = ref('企业微信登录失败，请重试')
const errorCode = ref('')

onMounted(async () => {
  // 根据用户权限初始化vConsole调试工具
  initVConsole()
  
  // 获取URL参数中的错误信息
  const error = route.query.error
  const code = route.query.code
  const message = route.query.message
  
  if (message) {
    errorMessage.value = decodeURIComponent(message)
  } else if (error) {
    switch (error) {
      case 'invalid_code':
        errorMessage.value = '授权码无效或已过期'
        break
      case 'network_error':
        errorMessage.value = '网络连接失败，请检查网络后重试'
        break
      case 'server_error':
        errorMessage.value = '服务器错误，请稍后重试'
        break
      case 'user_not_found':
        errorMessage.value = '用户不存在或未授权'
        break
      case 'access_denied':
        errorMessage.value = '用户拒绝授权'
        break
      default:
        errorMessage.value = '登录失败，请重试'
    }
  }
  
  if (code) {
    errorCode.value = code
  }
})

const retryLogin = () => {
  router.push('/wechat-work-login')
}

const goToNormalLogin = () => {
  router.push('/login-mobile')
}
</script>

<style scoped>
.wechat-error {
  min-height: 100vh;
  background: linear-gradient(135deg, #FF4757 0%, #FF3742 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.error-container {
  width: 100%;
  max-width: 400px;
}

.error-card {
  background: white;
  border-radius: 16px;
  padding: 40px 30px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.error-icon {
  margin-bottom: 20px;
}

.error-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 16px 0;
}

.error-message {
  font-size: 16px;
  color: #666;
  margin: 0 0 20px 0;
  line-height: 1.5;
}

.error-details {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 30px;
  text-align: left;
}

.error-details p {
  margin: 0;
  font-size: 14px;
  color: #666;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.retry-btn {
  background: #07C160;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 14px 24px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.retry-btn:hover {
  background: #06AD56;
  transform: translateY(-1px);
}

.retry-btn:active {
  transform: translateY(0);
}

.normal-login-btn {
  background: transparent;
  color: #666;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 14px 24px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.normal-login-btn:hover {
  background: #f8f9fa;
  border-color: #ccc;
}

.normal-login-btn:active {
  background: #e9ecef;
}

/* 移动端适配 */
@media (max-width: 480px) {
  .wechat-error {
    padding: 10px;
  }
  
  .error-card {
    padding: 30px 20px;
  }
  
  .error-title {
    font-size: 20px;
  }
  
  .action-buttons {
    gap: 10px;
  }
  
  .retry-btn,
  .normal-login-btn {
    padding: 12px 20px;
    font-size: 14px;
  }
}
</style>